using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using ScreenManager;
using Microsoft.Xna.Framework.Content;
using LabyrinthProject;

namespace LabirintGUI.Screens
{
    class MainScreen : ScreenManager.Screen
    {
        SpriteBatch MainSpriteBatch;
        Texture2D Background;
        MouseHandler ourMouse; //An instance of the MouseHandler class
        List<Button> buttonlist; //Our list of buttons
        MouseState mouseState; //MouseState needed for what checking 

        Button newGameButton; //An instance of Button        
        Button aboutButton; //An instance of Button       
        Button helpButton; //An instance of Button        
        Button settingsButton; //An instance of Button       
        Button exitButton; //An instance of Button
            

        public MainScreen(GraphicsDevice device)
            : base(device, "main")
        {
        }

        public override bool Init(Game game)
        {
            
            Background = game.Content.Load<Texture2D>(@"main");

            //new game button
            Texture2D textureNewGame= game.Content.Load<Texture2D>(@"new_game_button");

            Rectangle rectangleNewGame = new Rectangle(GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / 2 - (textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2, GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height * 3 / 9, textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                , textureNewGame.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);
            Texture2D newGameOnHover = game.Content.Load<Texture2D>(@"new_game_button_clicked"); //Sets texture to be used
            Texture2D newGameOnClick = game.Content.Load<Texture2D>(@"new_game_button_clicked"); //Sets texture to be used
            newGameButton = new Button(rectangleNewGame, textureNewGame, newGameOnHover, newGameOnClick, "new_game");

            //about button
            Texture2D textureAbout = game.Content.Load<Texture2D>(@"about_button");
            Rectangle rectangleAbout = new Rectangle(GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / 2 - (textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2, GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height * 6 / 9, textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                 , textureNewGame.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);
            Texture2D aboutOnHover = game.Content.Load<Texture2D>(@"about_button_clicked"); //Sets texture to be used
            Texture2D aboutOnClick = game.Content.Load<Texture2D>(@"about_button_clicked"); //Sets texture to be used
            aboutButton = new Button(rectangleAbout, textureAbout, aboutOnHover, aboutOnClick, "about");

            //settings button
            Texture2D textureSettings = game.Content.Load<Texture2D>(@"settings_button");
            Rectangle rectangleSettings = new Rectangle(GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / 2 - (textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2, GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height * 4 / 9, textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                            , textureNewGame.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);
            Texture2D settingsOnHover = game.Content.Load<Texture2D>(@"settings_button_clicked"); //Sets texture to be used
            Texture2D settingsOnClick = game.Content.Load<Texture2D>(@"settings_button_clicked"); //Sets texture to be used
            settingsButton = new Button(rectangleSettings, textureSettings, settingsOnHover, settingsOnClick, "settings");

            //help button
            Texture2D textureHelp = game.Content.Load<Texture2D>(@"help_button");
            Rectangle rectangleHelp = new Rectangle(GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / 2 - (textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2, GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height * 5 / 9, textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                            , textureNewGame.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);
            Texture2D helpOnHover = game.Content.Load<Texture2D>(@"help_button_clicked"); //Sets texture to be used
            Texture2D helpOnClick = game.Content.Load<Texture2D>(@"help_button_clicked"); //Sets texture to be used
            helpButton = new Button(rectangleHelp, textureHelp, helpOnHover, helpOnClick, "help");

            //exit button
            Texture2D textureExit = game.Content.Load<Texture2D>(@"exit_button");
            Rectangle rectangleExit = new Rectangle(GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / 2 - (textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2, GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height * 7 / 9, textureNewGame.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                            , textureNewGame.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);
            Texture2D exitOnHover = game.Content.Load<Texture2D>(@"exit_button_clicked"); //Sets texture to be used
            Texture2D exitOnClick = game.Content.Load<Texture2D>(@"exit_button_clicked"); //Sets texture to be used
            exitButton = new Button(rectangleExit, textureExit, exitOnHover, exitOnClick, "exit");
                      
            ourMouse = new MouseHandler(new Vector2(0,0), game.Content.Load<Texture2D>(@"pointer"));

            buttonlist = new List<Button>(); //Actually creating the list
            this.buttonlist.Add(newGameButton);
            this.buttonlist.Add(settingsButton);
            this.buttonlist.Add(helpButton);
            this.buttonlist.Add(aboutButton);
            this.buttonlist.Add(exitButton);

            return base.Init(game);
        }



        

        public override void Shutdown()
        {
            base.Shutdown();
        }



        public override void Draw(GameTime gameTime)
        {

            MainSpriteBatch = new SpriteBatch(_device);

            this.MainSpriteBatch.Begin();
            Vector2 position = new Vector2(0, 0);
            Rectangle backgroundRectangle = new Rectangle(0, 0, _device.Viewport.Width, _device.Viewport.Height);
            this.MainSpriteBatch.Draw(Background, backgroundRectangle, Color.White);


            foreach (Button b in buttonlist) //For every button run through the code between the {}
            {
                //For every button in our list, draw it
                b.Draw(MainSpriteBatch);

                if (b.hovering) //If we are hovering
                {
                    //Draw the hover sprite
                    MainSpriteBatch.Draw(b.OnHovering, b.position, Color.White );
                }
                if (b.clicking) //If we are clicking
                {
                    //Draw the clicking sprite.
                    MainSpriteBatch.Draw(b.OnClicking, b.position, Color.White);
                    switch (b.name)
                    {
                        case "new_game":
                            SCREEN_MANAGER.goto_screen("new_game_1");
                            break;
                        case "settings":
                            SCREEN_MANAGER.goto_screen("settings");
                            break;
                        case "help":
                            SCREEN_MANAGER.goto_screen("help");
                            break;
                        case "about":
                            SCREEN_MANAGER.goto_screen("about");
                            break;
                        case "exit":
                            _game.Exit();
                            break;
                    }     
                }
            }
            //Draw the mouse.
            ourMouse.Draw(MainSpriteBatch);
            //Done
            MainSpriteBatch.End();

            base.Draw(gameTime);
        }

        private void HandleMouse()
        {
            mouseState = Mouse.GetState(); //Get the current state of the mouse
            foreach (Button b in buttonlist)
            {                     
                if (ourMouse.ButtonClick(b)) //There's only one button, so we just hardcode it.
                    //If we're hovering over the mouse
                    b.hovering = true; //We ARE hovering
                else
                    b.hovering = false; //Not hovering.
                //We don't even need to use ButtonClick() again if we know if we're hovering.
                if (mouseState.LeftButton == ButtonState.Pressed && b.hovering) //If we're clicking with the Left Mouse Button and we're over the button.
                    b.clicking = true; //We ARE clicking
                else
                    b.clicking = false; //Not clicking
            }
        }


        public override void Update(GameTime gameTime)
        {
 
            // Check if m is pressed and go to screen2
            /*if (Keyboard.GetState().IsKeyDown(Keys.Back))
            {
                SCREEN_MANAGER.goto_screen("new_game");
            }*/
            HandleMouse(); //Check clicking
            ourMouse.Update();
            base.Update(gameTime);
        }
    }
}
